package com.sportswork.sportswork.controller.admin;

import com.sportswork.sportswork.config.SecurityUserInfo;
import com.sportswork.sportswork.core.entity.Role;
import com.sportswork.sportswork.core.entity.Student;
import com.sportswork.sportswork.core.entity.User;
import com.sportswork.sportswork.core.service.dto.PageDTO;
import com.sportswork.sportswork.core.service.dto.UserDTO;
import com.sportswork.sportswork.core.service.impl.StudentServiceImp;
import com.sportswork.sportswork.core.service.impl.UserServiceImp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author dengwei
 * @date 2020/2/3 12:11
 * @description
 */
@Slf4j
@Controller("studentController")
public class StudentController {
    @Resource
    private StudentServiceImp studentServiceImp;

    @Resource
    private UserServiceImp userServiceImp;

    @RequestMapping("/admin/student/list")
    public String List(){
        return "/pages/view/admin/student/list";
    }

    @RequestMapping("/admin/student/creditList")
    public String creditList(){
        return "/pages/view/admin/student/creditList";
    }

    @RequestMapping("/admin/student/getAllStudents")
    @ResponseBody
    public Object getAllStudents(@RequestParam String number,@RequestParam String name ,@RequestParam String sex,@RequestParam String rxnj){
       return new PageDTO<Student>().toPageDTO(studentServiceImp.getAllStudents(number,name,sex,rxnj));
        //return studentServiceImp.getAllStudents(number,name,sex,rxnj);
    }

    @RequestMapping(value = "/admin/student/add", method = RequestMethod.POST)
    @ResponseBody
    public String add(Student student) {
        studentServiceImp.addStudent(student);
        //添加经用户表  角色表
//        User user = new User();
//        user.setUsername(student.getName());
//        user.setPassword(student.getPassword());
//        user.setPhone(student.getPhone());
//        user.setRoleId("3");
//        UserDTO userDTO = new UserDTO(user);
//        userServiceImp.addUser(userDTO);

        return "success";
    }

    @RequestMapping(value = "/admin/student/getStudent", method = RequestMethod.POST)
    @ResponseBody
    public Student getStudent(String id) {
        return studentServiceImp.getStudent(id);
    }


    @RequestMapping(value = "/admin/student/edit", method = RequestMethod.POST)
    @ResponseBody
    public String edit(Student student) {
        studentServiceImp.setStudent(student);
        return "success";
    }



    @RequestMapping(value = "/admin/student/delete", method = RequestMethod.POST)
    @ResponseBody
    public String delete(@RequestParam String id) {
        studentServiceImp.deleteStudent(id);
        return "true";
    }

    @RequestMapping(value = "/admin/student/deletes", method = RequestMethod.POST)
    @ResponseBody
    public String deletes(@RequestParam String ids) {
        studentServiceImp.deleteStudents(ids);
        return "true";
    }



}
